var canshu =1;

var getValue=$("#shaixuan").find("option:selected").val()

var pic = 0

//判断
function asd(t) {
    if ("左上方"==$("#shaixuan").find("option:selected").val()){
        $('#more').text('左上方图片大小限制为230*80');
        $("#pinlei").attr("disabled","disabled");
        var moren='<option value="">'+'无'+'</option>'
        $("#pinlei").html(moren);

        $("#file").val("")
        $("#myImg").css("height","100px")
        $("#myImg").css("width","117.33px")
        $("#myImg").attr("src","../../../../static/h-ui.admin/images/20180302170728.png")
        pic=1
    }else if ("0"==$("#shaixuan").find("option:selected").val()){

        $('#more').text('');
        $("#pinlei").removeAttr("disabled");

        $("#file").val("")
        $("#myImg").css("height","100px")
        $("#myImg").css("width","117.33px")
        $("#myImg").attr("src","../../../../static/h-ui.admin/images/20180302170728.png")
        pic=1
    }else{
        $('#more').text('上下方图片大小限制为210*105');
        $("#pinlei").removeAttr("disabled");

        $("#file").val("")
        $("#myImg").css("height","100px")
        $("#myImg").css("width","117.33px")
        $("#myImg").attr("src","../../../../static/h-ui.admin/images/20180302170728.png")
        pic=1
        categorysss()
    }
}
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))
for(var i=0;i<warehouses.data.length;i++){
    if(warehouses.data[i].warehouseName=='北京仓'){
        $("#diyu").append("<option value=" + warehouses.data[i].warehouseCode + " selected>" + warehouses.data[i].warehouseName + "</option>")
    }else {
        $("#diyu").append("<option value=" + warehouses.data[i].warehouseCode + ">" + warehouses.data[i].warehouseName + "</option>")
    }

}

if (!getValue=="左上方"){
    categorysss()
}
function categorysss() {
    /* 左上方不可选择 */
    if ($("#shaixuan").find("option:selected").val()=="左上方"||$("#shaixuan").find("option:selected").val()=="0"){
        return;
    }
    var user_data = {
        platform:$("#pingtai").val(),
        warehouse:$("#diyu").val(),
        source:10
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if(data.errorCode == 0){
            var tab_data=data.data;
            var hang = '';
            if(tab_data == ''){
                hang = '<option value=""></option>'
                $("#pinlei").html(hang);
            }else{
                for(var i=0; i<tab_data.length; i++){
                    if (tab_data[i].display_content!=''){
                        hang += '<option value="'+tab_data[i].front_first_category_id+'">'+tab_data[i].display_content+'</option>'
                    }
                }
                $("#pinlei").html(hang);
            }
        }
    };
    web.doAjax(root.sroreinter.selectCatgoryIdAsPlatformWarehouse, 'post', user_data, succCallBack);
};

// 保存按钮
$("#form-member").validate({
    rules: {
        title:"titleCheck"
    },
    //提交表单数据
    submitHandler:function(form){

        if (!pic==0){
            layer.msg('请上传图片!', {icon: 6, time: 3000});
            return;
        }

        var formData = new FormData();
        if (web.readCookie("loginData")==undefined){
            layer.msg('清除缓存后请刷新页面重新登陆!', {icon: 6, time: 3000});
            return
        }
        formData.append("started_at",$("#logmin").val());
        formData.append("category_id",18);
        formData.append("end_in",$("#logmax").val());
        formData.append("image_url",$("#url").val());
        formData.append("title",$("#title").val());
        formData.append("file",$("#file")[0].files[0]);
        formData.append("config_location",$("#shaixuan").find("option:selected").val());

        formData.append("platform",$("#pingtai").find("option:selected").val());
        formData.append("warehouse_code",$("#diyu").find("option:selected").val());
        formData.append("warehouse_name",$("#diyu").find("option:selected").text());
        formData.append("front_first_category_id",$("#pinlei").find("option:selected").val());

        formData.append("created_name",JSON.parse(web.readCookie("loginData")).user.username);
        formData.append("created_by",JSON.parse(web.readCookie("loginData")).user.user_ID);
        console.log($("#file")[0].files[0])
        if ($("#shaixuan").find("option:selected").val()==0){
            layer.msg('请选择配置位置', {icon: 6, time: 3000});
            return false;
        }
        canshu=canshu+1;
        if (canshu>2){
            return false;
        }else{
            $.ajax({
                url: root.sroreinter.save_resource_conf,
                cache: false,  //禁用缓存
                type: "POST",
                // 告诉jQuery不要去处理发送的数据
                processData : false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType : false,
                data: formData,
                success: function (data) {
                    console.log(data)
                    if (data.errorCode=="92"){
                        layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
                        canshu=1
                    }
                    if (data.errorCode=="94"){
                        layer.msg(data.errorMessage, {icon: 6, time: 3000});
                        canshu=1
                    }
                    if(data.errorCode=="0") {
                        layer.msg('操作成功!', {icon: 6, time: 1000});
                        parent.queryDataFn();
                        setTimeout( function () {
                            cancelLayerNoRefreshFn();
                        },700);
                    }
                    if (data.errorCode=="93"){
                        layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                        canshu=1
                        return
                    }
                }
            });
        }
    }
})
function changImg(e){
    if ($("#shaixuan").find("option:selected").val()=='0'){
        layer.msg('请选择配置位置', {icon: 6, time: 2000});
        canshu=1
        $("#file").val("")
        return;
    }

    var image = new Image();
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 6, time: 2000});
            $("#file").val("")
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {
            image.src =e.target.result;
            image.onload = function() {
                if(file.size>5*1024*1024){
                    layer.msg('图片不得大于5M!', {icon: 6, time: 2000});
                    $("#file").val("")
                    canshu=1
                    return ;
                }else{
                    var element = $(e.target).next();
                    if ("左上方"==$("#shaixuan").find("option:selected").val()){
                        if (this.width!=230||this.height!=80){
                            layer.msg('左上方图片大小限制为230*80!', {icon: 6, time: 2000});
                            $("#file").val("")
                            return ;
                        } else {
                            $("#myImg").css({width:this.width+"px",height:this.height+"px",cursor:"pointer"});
                            $("#myImg").attr("src",e.target.result);
                            pic=0
                        }
                    }else{
                        if (this.width!=210||this.height!=105){
                            layer.msg('上下方图片大小限制为210*105!', {icon: 6, time: 2000});
                            $("#file").val("")
                            return ;
                        } else {
                            $("#myImg").css({width:this.width+"px",height:this.height+"px",cursor:"pointer"});
                            $("#myImg").attr("src",e.target.result);
                            pic=0
                        }
                    }
                }
            };
        }
    }
}
//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        //console.log(data)
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
$('#pic').click(function (event) {
    event.preventDefault()
    $('#file').click()
})
